home *** CD-ROM | disk | FTP | other *** search
-
- /* ---------------------------------------------------
- LISTING 2
-
- TEST16.C
- A driver used to validate the Ctrl-C/Break
- routines in ctrl_c.asm. The C standard
- library function calls use DOS Int 0x21 to
- obtain keys from the keyboard buffer and to
- display characters. This gives DOS plenty
- of opportunity to execute the Ctrl-C/Break
- checking code within interrupt 0x21.
-
- Commands to produce an executable from the code as
- shown in the listings:
- masm ctrl_c;
- cl -AL -c test16.c
- link test16 ctrl_c;
-
- Author: D. Burki
- -------------------------------------------------- */
-
- #include <stdio.h>
-
- /* here we define the key which will allow the input loop
- to terminate. this key must NOT be a cursor movement
- or function key. getch() must return non zero on the
- initial call when this key is pressed in order to exit
- the do ... while() loop. */
- /* exit when enter pressed*/
- #define EXIT_KEY 0x0d
- /* key name for inform() */
- #define EXIT_KEY_NAME "ENTER"
-
-
- int break_flag = 0; /* flag set if Ctrl-C or */
- /* Ctrl-Break is detected */
-
-
- void main(void);
- void inform(void);
-
- extern void capture(int far *);
- extern void release(void);
-
-
- /* ----- MAIN -----
- -------------------------------------------------- */
- void main( void )
- {
- unsigned int ch;
- /* install the traps */
- capture((int far *)&break_flag);
- inform();
-
- do /* until EXIT KEY pressed */
- {
- printf("If you press Ctrl-C/Break, you must also\n");
- printf("press another key. capture() will eat the \n");
- printf("Ctrl-C,and Ctrl-Break won't generate a key ");
- printf("stroke.\nPress any key, %s to exit.\n",
- EXIT_KEY_NAME);
- ch = getch();
- if( ch == 0 )
- {
- printf("Cursor movenent or function key\n\n");
- getch(); /* remove 2nd part of key */
- }
- else
- printf("Key pressed is \"%c\"\n\n",(char)ch);
- /* if flag has been set */
- if( break_flag )
- {
- printf("\n\tCTRL-C/BREAK DETECTED\n");
-
- /* NOTE: the application is responsible for resetting */
- /* the break flag after Ctrl-C/Break detected. */
- break_flag = 0;
- }
- } while( ch != EXIT_KEY );
- printf("\n\nExit Key detected. Exiting...\n");
- release(); /* uninstall the int 16h,
- 23h, and 1bh vectors */
- }
-
-
- /* ----- INFORM -----
- tell the user what is going to happen
- ----------------------------------------------------- */
- void inform()
- {
- int ch;
-
- printf("Ctrl-C and Ctrl-Break have been disabled.\n");
- printf("Press any key to see the keycode & character.\n");
- printf("Pressing Ctrl-C or Ctrl-Break will show that\n");
- printf("these keys have been trapped.\n\n");
- printf("Pressing Ctrl-2 or Alt-3 (keypad) will show\n");
- printf("^C and terminate the program. Pressing %s\n",
- EXIT_KEY_NAME);
- printf("will exit the input loop and restore the\n");
- printf("interrupt vectors.\n\n");
- printf("Press any key to begin\n\n");
- ch = getch(); /* wait till key pressed */
- if( ch == 0 ) /* if function or kursor */
- ch = getch(); /* remove 2nd half */
- }
-
-
-